/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewAthletDetails.java
 *
 * Created on 07.06.2009, 17:49:12
 */

package trackreggui;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import webreg.model.Athlet;
import webreg.model.Sex;
import webreg.model.exceptions.EmptyNameException;
import webreg.model.exceptions.EmptySurnameException;
import webreg.model.exceptions.NegativNumberException;
import webreg.model.swing.SexComboBoxModel;

/**
 *
 * @author STKNOLL
 */
public class ViewAthletDetails extends javax.swing.JDialog {

    /** Creates new form ViewAthletDetails */
    public ViewAthletDetails(java.awt.Frame parent) {
        super(parent);
        initComponents();
        mUseAthlet = null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        captionAthletLabel = new javax.swing.JLabel();
        captionSeparator = new javax.swing.JSeparator();
        nameTextField = new javax.swing.JTextField();
        surnameTextField = new javax.swing.JTextField();
        idNumberTextField = new javax.swing.JTextField();
        yearOfBirthTextField = new javax.swing.JTextField();
        ageClassificationComboBox = new javax.swing.JComboBox();
        sexComboBox = new javax.swing.JComboBox();
        surnameLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        yearOfBirthLabel = new javax.swing.JLabel();
        sexLabel = new javax.swing.JLabel();
        ageClassificationLabel = new javax.swing.JLabel();
        idLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAthletDetails.class);
        setTitle(resourceMap.getString("athletDetailsForm.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(318, 380));
        setModal(true);
        setName("athletDetailsForm"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        captionAthletLabel.setFont(resourceMap.getFont("captionAthletLabel.font")); // NOI18N
        captionAthletLabel.setText(resourceMap.getString("captionAthletLabel.text")); // NOI18N
        captionAthletLabel.setName("captionAthletLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 10);
        getContentPane().add(captionAthletLabel, gridBagConstraints);

        captionSeparator.setName("captionSeparator"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 5, 10);
        getContentPane().add(captionSeparator, gridBagConstraints);

        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(nameTextField, gridBagConstraints);

        surnameTextField.setText(resourceMap.getString("surnameTextField.text")); // NOI18N
        surnameTextField.setName("surnameTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(surnameTextField, gridBagConstraints);

        idNumberTextField.setText(resourceMap.getString("idNumberTextField.text")); // NOI18N
        idNumberTextField.setName("idNumberTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(idNumberTextField, gridBagConstraints);

        yearOfBirthTextField.setText(resourceMap.getString("yearOfBirthTextField.text")); // NOI18N
        yearOfBirthTextField.setName("yearOfBirthTextField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(yearOfBirthTextField, gridBagConstraints);

        ageClassificationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        ageClassificationComboBox.setEnabled(false);
        ageClassificationComboBox.setName("ageClassificationComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageClassificationComboBox, gridBagConstraints);

        sexComboBox.setModel(new SexComboBoxModel());
        sexComboBox.setName("sexComboBox"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(sexComboBox, gridBagConstraints);

        surnameLabel.setText(resourceMap.getString("surnameLabel.text")); // NOI18N
        surnameLabel.setName("surnameLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(surnameLabel, gridBagConstraints);

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(nameLabel, gridBagConstraints);

        yearOfBirthLabel.setText(resourceMap.getString("yearOfBirthLabel.text")); // NOI18N
        yearOfBirthLabel.setName("yearOfBirthLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(yearOfBirthLabel, gridBagConstraints);

        sexLabel.setText(resourceMap.getString("sexLabel.text")); // NOI18N
        sexLabel.setName("sexLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(sexLabel, gridBagConstraints);

        ageClassificationLabel.setText(resourceMap.getString("ageClassificationLabel.text")); // NOI18N
        ageClassificationLabel.setEnabled(false);
        ageClassificationLabel.setName("ageClassificationLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(ageClassificationLabel, gridBagConstraints);

        idLabel.setText(resourceMap.getString("idLabel.text")); // NOI18N
        idLabel.setName("idLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(idLabel, gridBagConstraints);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getActionMap(ViewAthletDetails.class, this);
        okButton.setAction(actionMap.get("createAthlet")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setMaximumSize(new java.awt.Dimension(87, 23));
        okButton.setMinimumSize(new java.awt.Dimension(87, 23));
        okButton.setName("okButton"); // NOI18N
        okButton.setPreferredSize(new java.awt.Dimension(87, 23));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(okButton, gridBagConstraints);

        cancelButton.setAction(actionMap.get("closeAthletDetails")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.weighty = 0.125;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(cancelButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void createAthlet() {
        try {
            mUseAthlet = new Athlet();
            mUseAthlet.setId(idNumberTextField.getText());
            mUseAthlet.setSurname(surnameTextField.getText());
            mUseAthlet.setName(nameTextField.getText());
            mUseAthlet.setYearOfBirth(yearOfBirthTextField.getText());
            Sex selectedSex = (Sex) sexComboBox.getSelectedItem();
            mUseAthlet.setSex(selectedSex);
            closeAthletDetails();
        } catch (EmptySurnameException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAthletDetails.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptySurname.text"), resourceMap.getString("errorEmptySurname.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewAthletDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EmptyNameException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAthletDetails.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorEmptyName.text"), resourceMap.getString("errorEmptyName.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewAthletDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NegativNumberException ex) {
            org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(trackreggui.TrackRegGuiApp.class).getContext().getResourceMap(ViewAthletDetails.class);
            JOptionPane.showMessageDialog(null, resourceMap.getString("errorNumber.text"), resourceMap.getString("errorNumber.title"), JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(ViewAthletDetails.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    @Action
    public void closeAthletDetails() {
        setVisible(false);
    }

    public void setAthlet(Athlet inAthlet) {
        idNumberTextField.setText( inAthlet.getIdAsString() );
        surnameTextField.setText( inAthlet.getSurname());
        nameTextField.setText( inAthlet.getName() );
        yearOfBirthTextField.setText( inAthlet.getYearOfBirthAsString() );
        sexComboBox.setSelectedItem( inAthlet.getSex() );
    }

    public Athlet getAthlet() {
        return mUseAthlet;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ageClassificationComboBox;
    private javax.swing.JLabel ageClassificationLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel captionAthletLabel;
    private javax.swing.JSeparator captionSeparator;
    private javax.swing.JLabel idLabel;
    private javax.swing.JTextField idNumberTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox sexComboBox;
    private javax.swing.JLabel sexLabel;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JTextField surnameTextField;
    private javax.swing.JLabel yearOfBirthLabel;
    private javax.swing.JTextField yearOfBirthTextField;
    // End of variables declaration//GEN-END:variables

    private Athlet mUseAthlet;
}
